//
//  DHSubscribeManagerController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/14.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSubscribeManagerController.h"
#import "DHSubscribeManager_Cell.h"
#import "DHSubscribe_BackController.h"

@interface DHSubscribeManagerController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIButton *ensureBackButton;

@property (strong, nonatomic) UIButton *productBackButton;

@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) NSMutableArray *orderArr;
@property (strong, nonatomic) NSMutableArray *selsectedArr;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewHeightLayout;

@end

@implementation DHSubscribeManagerController

- (NSMutableArray *)orderArr{
    if (!_orderArr) {
        _orderArr = [NSMutableArray array];
    }
    return _orderArr;
}

- (NSMutableArray *)selsectedArr{
    if (!_selsectedArr) {
        _selsectedArr = [NSMutableArray array];
    }
    return _selsectedArr;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.selsectedArr removeAllObjects];
    [self.productBackButton setTitle:@"归还" forState:UIControlStateNormal];
    self.bottomView.hidden = YES;
    self.bottomViewHeightLayout.constant = 0;
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"订阅管理";
    
    self.bottomView.hidden = YES;
    
    self.productBackButton = [[UIButton alloc] initWithFrame:CGRectMake(self.navView.width - 54, 20, 44, 44)];
    [self.productBackButton setTitle:@"归还" forState:UIControlStateNormal];
    self.productBackButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.productBackButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.navView addSubview:self.productBackButton];
    [self.productBackButton bk_addEventHandler:^(id sender) {
        if (self.orderArr.count == 0) {
            [NSObject showHudTipStr:@"没有待归还服饰"];
            return ;
        }
        if (self.bottomView.hidden) {
            [self.productBackButton setTitle:@"取消" forState:UIControlStateNormal];
            self.bottomView.hidden = NO;
            self.bottomViewHeightLayout.constant = 50;
            [self.tableView reloadData];
        }else{
            [self.productBackButton setTitle:@"归还" forState:UIControlStateNormal];
            self.bottomView.hidden = YES;
            self.bottomViewHeightLayout.constant = 0;
            [self.tableView reloadData];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.ensureBackButton.layer.cornerRadius = self.ensureBackButton.height / 2;
    WeakSelf(weakSelf)
    [self.ensureBackButton bk_addEventHandler:^(id sender) {
        DHSubscribe_BackController *vc = [[DHSubscribe_BackController alloc] init];
        vc.arr = weakSelf.selsectedArr;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    self.bottomView.layer.cornerRadius = 2;
    self.bottomView.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.bottomView.layer.shadowRadius = 5;
    self.bottomView.layer.shadowOpacity = 0.5;
    self.bottomView.layer.shadowOffset  = CGSizeMake(0, 0);
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHSubscribeManager_Cell" bundle:nil] forCellReuseIdentifier:kDHSubscribeManager_Cell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    
    self.bottomViewHeightLayout.constant = 0;
}

- (void)loadData{
    [[DHNetAPIClient shareDHNetAPIClient] request_user_products_returnAndBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.orderArr removeAllObjects];
        [self.orderArr addObjectsFromArray:[data objectForKey:@"items"]];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        [self.tableView reloadData];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 132;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHSubscribeManager_Cell *cell = [tableView dequeueReusableCellWithIdentifier:kDHSubscribeManager_Cell forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSDictionary *dic = [self.orderArr objectAtIndex:indexPath.row];
    [cell updateCell:dic];
    if ([self.productBackButton.titleLabel.text isEqualToString:@"归还"]) {
        cell.selectedButtonWidthLayout.constant = 0;
    }else{
        if ([[dic objectForKey:@"status"] isEqualToString:@"待归还"]) {
            cell.selectedButtonWidthLayout.constant = 40;
            if ([self.selsectedArr containsObject:dic]) {
                [cell.selectedButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
            }else{
                [cell.selectedButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
            }
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [self.orderArr objectAtIndex:indexPath.row];
    if ([[dic objectForKey:@"status"] isEqualToString:@"待归还"]) {
        DHSubscribeManager_Cell *cell = (DHSubscribeManager_Cell *)[tableView cellForRowAtIndexPath:indexPath];
        if ([self.selsectedArr containsObject:dic]) {
            [self.selsectedArr removeObject:dic];
            [cell.selectedButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
        }else{
            [self.selsectedArr addObject:dic];
            [cell.selectedButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        }
    }
}

@end
